import React, { useEffect, useState } from 'react';
import { getArticleList } from "../../store/counterSlice";
import { useSelector, useDispatch } from 'react-redux'
import instance from "../../apis/request";
import { Table,
    Space,
    Button,Input,Modal 
 } from "antd";

 import Articleadd from "../../cpmputed/Articleadd";
 import Search from "../../cpmputed/Search";
function Article() {
    const {articleData,count} = useSelector(state => state.counter)
  const dispatch = useDispatch()
  const [data,setData]=useState({
    page:1,//页码  必传
pageSize:6,//每页显示条数 必传
keyword:null,//搜索标题
tag:null//搜索标签

  })
  const [fy,setFy]=useState({
    current:1,
    pageSize:6,
    total:0
  })
  const aa=async()=>{
    const res=await instance.get('/article/list',{
        params:data
    }).then(res=>{
        console.log(res);
        
        dispatch(getArticleList(res.data))
        setFy({
            ...fy,
            total:res.count
        })
    })
  }
  useEffect(()=>{
    aa()
  },[data,dispatch])
  const dd=(diss)=>{
    setData({
        page:diss.current,
        pageSize:diss.pageSize
    })
    setFy({
        current:diss.current,
        pageSize:diss.pageSize,
        total:diss.count
    })
  }
  const columns = [
  {
    title: 'ID',
    dataIndex: 'id',
    key: 'id',
  },
  {
    title: 'First Name',
    dataIndex: 'title',
    key: 'title',
  },
  {
    title: 'Last Name',
    dataIndex: 'content',
    key: 'content',
  },
  {
    title: 'username',
    dataIndex: 'createdAt',
    key: 'createdAt',
  },
  {
    title: 'Action',
    key: 'action',
    render: (_, record) => (
      <Space size="middle">
        <Button  onClick={()=>showModal(true,record)}>编辑</Button>
        <Button  type='primary' danger>删除</Button>
      </Space>
    ),
  },
];


const [isModalOpen, setIsModalOpen] = useState(false);
const [isShow,setIsShow]=useState(false)
const [comment,setComment]=useState('')

  const showModal = (idsd,record) => {
    setIsModalOpen(true);
    if(idsd){
        setIsShow(true)
        setComment(record)
    }else{
        setIsShow(false)

    }
  };

  const handleOk = () => {
    setIsModalOpen(false);
  };

  const handleCancel = () => {
    setIsModalOpen(false);
  };
  const onchangeSou=(k)=>{
    setData({
        ...data,
        keyword:k.keyword
    })
  }
    return (
        <div>
            <Button type='primary' onClick={()=>showModal()}>新增</Button>
            <Search kkk={onchangeSou} />
            <Table dataSource={articleData} columns={columns} pagination={fy} onChange={dd} />
            <Modal
        title={isShow?'编辑':'新增'}
        closable={{ 'aria-label': 'Custom Close Button' }}
        open={isModalOpen}
        onOk={handleOk}
        onCancel={handleCancel}
      >
        <Articleadd isShow={isShow} comment={comment} setData={setData} handleCancel={handleCancel} />
      </Modal>
        </div>
    );
}

export default Article;